package com.winit.config.service;

import com.winit.config.manager.ConfigManager;
import com.winit.config.model.Config;
import com.winit.config.repository.IConfigRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Map;

@Component
public class ConfigServiceImpl implements IConfigService {
   /* , ApplicationContextAware, InitializingBean */
//    /**
//     * 该引用只在bean初始化时可用，即在方法this.afterPropertiesSet()中可用
//     */
//    private ApplicationContext applicationContext = null;

    private static Logger log = LoggerFactory.getLogger(ConfigServiceImpl.class);

    @Autowired
    private IConfigRepository<Config, String> configRepository;
    @Autowired
    private ConfigManager configManager;

//    @Override
//    public void setApplicationContext(ApplicationContext applicationContext) {
//        this.applicationContext = applicationContext;
//    }

//    @Override
//    public void afterPropertiesSet() throws Exception {
//        configRepository = this.applicationContext.getBean(IConfigRepository.class);
//        if (null == configRepository){
//            throw new Exception("initialization exp, an instance of IConfigRepository expected, but get nothing.");
//        }
//        //销毁applicationContext引用
//        applicationContext = null;
//    }

    @Override
    public Config saveConfig(String id, String parentId, Map<String, String> kvMap, Map<String, String> kRefMap, Map<String, String> kChangeMap) {
        Config saveOne = new Config();
        saveOne.setId(id);
        saveOne.setParentId(parentId);
        saveOne.setKVMap(kvMap);
        saveOne.setKRefMap(kRefMap);
        saveOne.setKChangeMap(kChangeMap);
        //到这里即为更新config,注意这里是不允许更新parentId的
        return this.saveConfig(saveOne);
    }

    @Override
    public Config saveConfig(Config config) {
        return saveConfig(config, true);
    }

    @Override
    public Config saveConfig(Config config, boolean isMerge) {
        if (null == config.getId()) {    //新增config
            return this.configManager.createConfig(config);
        }
        //到这里即为更新config,注意这里是不允许更新parentId的
        return this.configManager.updateConfigById(config, isMerge);
    }

//    //unused
//    @Override
//    public Map<String, String> merge(Map<String, String> kvMap1, Map<String, String> kvMap2, Map<String, String> kChangeMap) {
//        return null;
//    }
//    //unused
//    @Override
//    public void render(Map<String, String> kvMap, Map<String, String> kRefMap) {
//
//    }

    @Override
    public Config getConfigMap(String id, boolean rawConfig) throws Exception {
        if (rawConfig) {     //返回数据库记录
            return this.configRepository.findOne(id);
        }
        return this.configManager.getConfigDetailById(id);
    }

    @Override
    public Integer deleteConfigById(String id) {
        if (StringUtils.isEmpty(id)){
            return 0;
        }
        this.configRepository.delete(id);
        return 1;
    }

    @Override
    public void mergeConfigById(String priorityId, String destId, boolean raw) throws Exception {
        this.configManager.mergeConfigById(priorityId, destId, raw);
    }
}
